Queries on a permutation with key [BIT, Fenwick Tree]

Time: O(NLogN); Space: O(N); medium

Given the array queries of positive integers between 1 and m, you have to process all queries[i]

(from i=0 to i=queries.length-1)

according to the following rules:

  • In the beginning, you have the permutation P=[1,2,3,…,m].

  • For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i].

Return an array containing the result for the given queries.

Example 1:

Input: queries = [3,1,2,1], m = 5

Output: [2,1,2,1]

Explanation:

  • The queries are processed as follow:

    • For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5].

    • For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5].

    • For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5].

    • For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5].

  • Therefore, the array containing the result is [2,1,2,1].

Example 2:

Input: queries = [4,1,2,2], m = 4

Output: [3,1,2,0]

Example 3:

Input: queries = [7,5,5,8,3], m = 8

Output: [6,5,0,7,5]

Constraints:

  • 1 <= m <= 10^3

  • 1 <= queries.length <= m

  • 1 <= queries[i] <= m

Hints:

  1. Create the permutation P=[1,2,…,m], it could be a list for example.

  2. For each i, find the position of queries[i] with a simple scan over P and then move this to the beginning.

[1]:
class BIT(object):  # Fenwick Tree, 1-indexed
    def __init__(self, n):
        self.__bit = [0] * n

    def add(self, i, val):
        while i < len(self.__bit):
            self.__bit[i] += val
            i += (i & -i)

    def sum(self, i):
        result = 0
        while i > 0:
            result += self.__bit[i]
            i -= (i & -i)
        return result
[2]:
class Solution1(object):
    """
    Time: O(NLogN)
    Space: O(N)
    """
    def processQueries(self, queries, m):
        """
        :type queries: List[int]
        :type m: int
        :rtype: List[int]
        """
        bit = BIT(2 * m + 1)
        lookup = {}

        for i in range(1, m + 1):
            bit.add(m + i, 1)
            lookup[i] = m+i

        result, curr = [], m

        for q in queries:
            i = lookup.pop(q)
            result.append(bit.sum(i-1))
            bit.add(i, -1)
            lookup[q] = curr
            bit.add(curr, 1)
            curr -= 1

        return result
[3]:
s = Solution1()
queries = [3,1,2,1]
m = 5
assert s.processQueries(queries, m) == [2,1,2,1]

queries = [4,1,2,2]
m = 4
assert s.processQueries(queries, m) == [3,1,2,0]

queries = [7,5,5,8,3]
m = 8
assert s.processQueries(queries, m) ==  [6,5,0,7,5]